package com.hmdp;

import com.hmdp.utils.RedisIdWorker;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.data.redis.core.StringRedisTemplate;

import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneOffset;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * @BelongsProject: hm-dianping
 * @BelongsPackage: com.hmdp
 * @Author: jiangyi
 * @CreateTime: 2025-09-23  10:29
 * @Description: TODO
 * @Version: 1.0
 */
@SpringBootTest
public class RedisId {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Test
    public void testIdWorker() throws InterruptedException {
        RedisIdWorker redisIdWorker = new RedisIdWorker(stringRedisTemplate);

        ExecutorService es = Executors.newFixedThreadPool(300);
        CountDownLatch latch = new CountDownLatch(300);

        Runnable task = () -> {
            for (int i = 0; i < 100; i++) {
                long id = redisIdWorker.nextId("order");
                System.out.println("id = " + id);
            }
            latch.countDown();
        };

        long begin = System.currentTimeMillis();
        for (int i = 0; i < 300; i++) {
            es.submit(task);
        }
        latch.await();
        long end = System.currentTimeMillis();
        System.out.println("time = " + (end - begin));
    }

    public static void main(String[] args) throws InterruptedException {
        SpringApplication.run(RedisId.class, args).getBean(RedisId.class).testIdWorker();
    }
}

